

package top.continew.admin.question.manger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import top.continew.admin.question.manger.service.model.DelQuery;
import top.continew.admin.question.mapper.question.QuestionMapper;
import top.continew.admin.question.model.question.Question;
import top.continew.admin.question.model.work.WorkTemplateQuestionPool;
import top.continew.admin.question.manger.service.WorkService;
import top.continew.starter.core.validation.ValidationUtils;

import java.util.Objects;

@Service

public class WorkServiceImpl implements WorkService {

    @Autowired
    private StringRedisTemplate template;

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public void addWork(Long workTemplateId, Long questionId, Double score) {
        template.delete(Objects.requireNonNull(template.keys("*WorkTemplateQuestionPool*")));
        WorkTemplateQuestionPool workTemplateQuestionPool = new WorkTemplateQuestionPool()
            .selectOne(new QueryWrapper<WorkTemplateQuestionPool>().eq("wid", workTemplateId).eq("qid", questionId));
        if (workTemplateQuestionPool != null) {
            workTemplateQuestionPool.setUtime(System.currentTimeMillis());
            workTemplateQuestionPool.setScore(score);
            workTemplateQuestionPool.update(new QueryWrapper<WorkTemplateQuestionPool>().eq("wid", workTemplateId)
                .eq("qid", questionId));
            return;
        }
        workTemplateQuestionPool = new WorkTemplateQuestionPool();
        workTemplateQuestionPool.setWid(workTemplateId);
        workTemplateQuestionPool.setQid(questionId);
        workTemplateQuestionPool.setCtime(System.currentTimeMillis());
        workTemplateQuestionPool.setUtime(System.currentTimeMillis());
        workTemplateQuestionPool.setType(1);
        workTemplateQuestionPool.setStatus(0);
        workTemplateQuestionPool.setQuestiontype((byte)1);
        workTemplateQuestionPool.setScore(score);
        workTemplateQuestionPool.setSort(System.currentTimeMillis());
        workTemplateQuestionPool.insert();
    }

    @Override
    public Double getWorkScore(Long workTemplateId, Long questionId) {
        WorkTemplateQuestionPool workTemplateQuestionPool = new WorkTemplateQuestionPool()
            .selectOne(new QueryWrapper<WorkTemplateQuestionPool>().eq("wid", workTemplateId).eq("qid", questionId));
        if (workTemplateQuestionPool == null) {
            return 0.00;
        }
        return workTemplateQuestionPool.getScore();
    }

    @Override
    public void delQuestion(DelQuery delQuery) {
        ValidationUtils.throwIf(delQuery.getIds() != null && !delQuery.getIds().isEmpty(), "无可删除内容");
        LambdaQueryWrapper<Question> delWhere = new LambdaQueryWrapper<>();
        delWhere.in(Question::getId, delQuery.getIds());
        delWhere.eq(Question::getStatus, Question.STATUS_CREATE);
        this.questionMapper.delete(delWhere);
    }
}
